package com.cn.wanix.servlet.product;

import com.alibaba.fastjson.JSON;
import com.cn.wanix.model.ProductModel;
import com.cn.wanix.model.ResultModel;
import com.cn.wanix.service.ProductService;
import com.cn.wanix.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        0.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        ProductService productService = new ProductServiceImpl();
        //        4.返回一个结果
        ResultModel resultModel = productService.findAll(new ProductModel());
//        5.返回页面状态
//        PrintWriter out = resp.getWriter();
        String resJSON = JSON.toJSONString(resultModel);     // 转换为json
        resp.getWriter().print(resJSON); // 输出
    }
}
